//TESH.scrollpos=147
//TESH.alwaysfold=0
library ItemSystem initializer Init requires PropertySystem ,GameSystem,EquitPoolSystem

    globals
        private constant integer ITEM_ADD_VALUE_BUMBER = 3
        private constant real S_DROP_RATE_EQUIP_TRAIT_MAX = 1
        private constant real S_DROP_RATE_EQUIP_TRAIT_1 = 0.35
        private constant real S_DROP_RATE_EQUIP_TRAIT_2 = 0.12
        private constant real S_DROP_RATE_EQUIP_TRAIT_3 = 0.03
        private constant real S_DROP_RATE_EQUIP_TRAIT_4 = 0.01
        private itempool EquipTypePool = CreateItemPool()
    endglobals
    
    private function GetAddValue takes integer itemLv returns integer
        return itemLv * ITEM_ADD_VALUE_BUMBER
    endfunction

    // 获取基础值 通过物品等级与物品品级
    private function GetItemBaseValue takes integer rating, integer itemLv returns integer
        local integer addValue = 3
        set addValue =GetAddValue(itemLv)
        return  R2I(itemLv * addValue * 0.5 ) + itemLv * 5 + rating * addValue
    endfunction

    // 0=头部 1=肩膀 2=胸甲 3=手腕 4=手部 5=腰部 6=腿部 7=脚部 8=护符 9=戒指 10=副手 11=武器
    private function GetDefValue takes boolean isMainValue, integer itemType, integer rating, integer itemLv returns integer
        local integer addValue = GetAddValue(itemLv)
        local integer value = GetItemBaseValue(rating,itemLv)
        if isMainValue == false then
            set value = R2I(value*0.5)
            set addValue = R2I(addValue*0.5)
        else
            if itemType ==  2 then
                set value = R2I(value*2.15)
            elseif itemType == 6 then
                set value = R2I(value*1.65)
            elseif itemType == 7 then
                set value = R2I(value*1.25)
            endif
        endif    
        return GetRandomInt(value, value+addValue)
    endfunction

    // 获取属性数值
    private function GetAttributeValue takes boolean isMainValue,integer rating, integer itemLv returns integer
        local integer lv = itemLv * 5
        local integer addValue = GetAddValue(itemLv)
        local integer value = lv + addValue + lv * rating
        if isMainValue == false then
            set value = R2I(value * 0.5)
        endif
        return GetRandomInt(value, value+itemLv)
    endfunction

    // 获取攻击数值 weaponType武器类型  1=单手 2=双手  
    private function GetAtkValue takes boolean isMainValue,integer rating,integer itemType,integer weaponType, integer itemLv returns integer
        local integer lv = itemLv * 5
        local integer addValue = GetAddValue(itemLv)
        local integer value = lv + addValue + lv * rating
        if isMainValue == false then
            set value = R2I(value * 0.1)
            set addValue = R2I(addValue*0.1)
            if value <1 then 
                set value = 1
            endif
            if addValue<1 then
                set addValue = 1
            endif
        endif
        if weaponType == 1 then
        elseif weaponType == 2 then
        endif
        return GetRandomInt(value, value+addValue)
    endfunction
    
        // 获得装备类型
    function GetEquipType takes handle h returns integer
        return GetPropertyAtInt(h, "EquipType")
    endfunction
    
    // 获得装备类型
    function SetEquipType takes handle h, integer value returns nothing
        call SetPropertyAtInt(h,"EquipType",value)
    endfunction
    
    // 获得装备品质
    function GetEquipTrait takes handle h returns integer
        return GetPropertyAtInt(h, "EquipTrait")
    endfunction
    
    // 获得装备品质
    function SetEquipTrait takes handle h, integer value returns nothing
        call SetPropertyAtInt(h,"EquipTrait",value)
    endfunction
    
    // 0=头部 1=肩膀 2=胸甲 3=手部 4=腰部 5=腿部 6=脚部 7=护符 8=戒指 9=副手 10=武器
    private function GetEquipTypeName takes integer t returns string 
        if t == 0 then
            return "头部"
        elseif t == 1 then
            return "肩部"
        elseif t == 2 then
            return "胸部"
        elseif t == 3 then
            return "手部"
        elseif t == 4 then
            return "腰部"
        elseif t == 5 then
            return "腿部"
        elseif t == 6 then
            return "脚部"
        elseif t == 7 then
            return "护符"
        elseif t == 8 then
            return "戒指"
        elseif t == 9 then
            return "副手"
        elseif t == 10 then
            return "武器"
        endif
        return "未知"
    endfunction
    
    // 给装备随机一个品质 0=普通 1=优质 2=精良 3=传奇 4=套装
    private function GetEquipTraitName takes integer t returns string 
        if t == 0 then
            return "普通"
        elseif t == 1 then
            return "|cff00ff00优质|r"
        elseif t == 2 then
            return "|cff3366ff精良|r"
        elseif t == 3 then
            return "|cffff9900传奇|r"
        elseif t == 4 then
            return "|cffff00ff套装|r"
        endif
        return "未知"
    endfunction
    
    // 给装备随机一个品质 0=普通 1=优质 2=精良 3=传奇 4=套装
    private function UpdateEquipTrait takes item i, unit u returns nothing 
        local real dropRate = S_GAME_DIFFICULTY_LEVEL + GetDropRate(u)
        if GetRandomReal(0,S_DROP_RATE_EQUIP_TRAIT_MAX) <= S_DROP_RATE_EQUIP_TRAIT_4 * dropRate then
            call Log("当前套装掉落:"+I2S(R2I(S_DROP_RATE_EQUIP_TRAIT_4*dropRate*100))+"%")
            call SetEquipTrait(i,4)
        elseif GetRandomReal(0,S_DROP_RATE_EQUIP_TRAIT_MAX) <= S_DROP_RATE_EQUIP_TRAIT_3 * dropRate then
            call Log("当前传奇掉落:"+I2S(R2I(S_DROP_RATE_EQUIP_TRAIT_3*dropRate*100))+"%")
            call SetEquipTrait(i,3)
        elseif GetRandomReal(0,S_DROP_RATE_EQUIP_TRAIT_MAX) <= S_DROP_RATE_EQUIP_TRAIT_2 * dropRate then
            call Log("当前精良掉落:"+I2S(R2I(S_DROP_RATE_EQUIP_TRAIT_2*dropRate*100))+"%")
            call SetEquipTrait(i,2)
        elseif GetRandomReal(0,S_DROP_RATE_EQUIP_TRAIT_MAX) <= S_DROP_RATE_EQUIP_TRAIT_1 * dropRate then
            call Log("当前优质掉落:"+I2S(R2I(S_DROP_RATE_EQUIP_TRAIT_1*dropRate*100))+"%")
            call SetEquipTrait(i,1)
        else
            call Log("当前普通掉落:"+I2S(R2I(dropRate*100))+"%")
            call SetEquipTrait(i,0)
        endif
    endfunction
    
    private function UpateItemTips takes item i returns nothing
        local integer itype = GetItemTypeId(i)
        local string tips = GetEquipTypeName(GetEquipType(i)) + " " + GetEquipTraitName(GetEquipTrait(i)) + "|n"
        call YDWESetItemDataString(itype, 3, tips)
        call YDWESetItemDataString(itype, 2, tips)
        call Log("创建一个["+GetEquipTypeName(0)+":"+GetEquipTraitName(GetEquipTrait(i)) +"]物品")
    endfunction 
    
    // 设置物品的提示信息
    private function CreateItemData takes unit u,integer etype returns nothing
        local integer et = GetEquitTypeId()
        local item equip = CreateItem(et, 0, 0)
        call YDWESetItemDataString(et, 4, "这是一件代码创建出来的物品")
        call SetItemUserData(equip,GetHandleId(equip))
        call SetEquipType(equip,etype)
        call UpdateEquipTrait(equip,u)
        call UnitAddItem(GetTriggerUnit(),equip)
        call UpateItemTips(equip)
    endfunction
    
    //  任意单位获取物品触发动作
    private function GetItemTriggerAction takes nothing returns nothing
        local unit u = GetTriggerUnit()
        local item iobj = GetManipulatedItem()
        if GetItemTypeId(iobj)=='ET00' then
            call CreateItemData(u,0)
        elseif GetItemTypeId(iobj)=='ET01' then
            call CreateItemData(u,1)
        elseif GetItemTypeId(iobj)=='ET02' then
            call CreateItemData(u,2)
        elseif GetItemTypeId(iobj)=='ET03' then
            call CreateItemData(u,3)
        elseif GetItemTypeId(iobj)=='ET04' then
            call CreateItemData(u,4)
        elseif GetItemTypeId(iobj)=='ET05' then
            call CreateItemData(u,5)
        elseif GetItemTypeId(iobj)=='ET06' then
            call CreateItemData(u,6)
        elseif GetItemTypeId(iobj)=='ET07' then
            call CreateItemData(u,7)
        elseif GetItemTypeId(iobj)=='ET08' then
            call CreateItemData(u,8)
        elseif GetItemTypeId(iobj)=='ET09' then
            call CreateItemData(u,9)
        elseif GetItemTypeId(iobj)=='ET0A' then
            call CreateItemData(u,10)
        elseif GetItemTypeId(iobj)=='ET0B' then
            call CreateItemData(u,11)
        endif
    endfunction
    
    function Test takes nothing returns nothing
        local real x = 0
        local real y = 0
        local integer i = 0
        local item iobj
        loop
        exitwhen i > 100
            set x = GetRandomReal(GetRectMinX(bj_mapInitialPlayableArea), GetRectMaxX(bj_mapInitialPlayableArea))
            set y = GetRandomReal(GetRectMinY(bj_mapInitialPlayableArea), GetRectMaxY(bj_mapInitialPlayableArea))
            set iobj = PlaceRandomItem(EquipTypePool, x*0.1, y*0.1)
            set i = i + 1
        endloop
    endfunction
    
    
    private function Init takes nothing returns nothing
        
        local trigger getItemTrigger = CreateTrigger()
        
        call Log("装备系统初始化")
        
        call TriggerRegisterAnyUnitEventBJ(getItemTrigger, EVENT_PLAYER_UNIT_PICKUP_ITEM)
        call TriggerAddAction(getItemTrigger, function GetItemTriggerAction)
        call ItemPoolAddItemType(EquipTypePool,'ET00',5)
        call ItemPoolAddItemType(EquipTypePool,'ET01',5)
        call ItemPoolAddItemType(EquipTypePool,'ET02',3)
        call ItemPoolAddItemType(EquipTypePool,'ET03',5)
        call ItemPoolAddItemType(EquipTypePool,'ET04',5)
        call ItemPoolAddItemType(EquipTypePool,'ET05',5)
        call ItemPoolAddItemType(EquipTypePool,'ET06',3)
        call ItemPoolAddItemType(EquipTypePool,'ET07',5)
        call ItemPoolAddItemType(EquipTypePool,'ET08',1.5)
        call ItemPoolAddItemType(EquipTypePool,'ET09',1.5)
        call ItemPoolAddItemType(EquipTypePool,'ET0A',2)
        call ItemPoolAddItemType(EquipTypePool,'ET0B',1)
        
        debug call Test()
       
        
    endfunction


endlibrary
